package com.rem.chat.common.utils;

import com.rem.chat.common.constants.Constants;
import com.rem.chat.pojo.dataobject.AppUpdateDO;
import com.rem.chat.pojo.dataobject.GroupInfoDO;
import com.rem.chat.pojo.dataobject.UserBeautyIdDO;
import com.rem.chat.pojo.dataobject.UserInfoDO;
import com.rem.chat.pojo.request.GroupSaveRequest;
import com.rem.chat.pojo.request.SysSettingSaveRequest;
import com.rem.chat.pojo.response.*;
import com.rem.chat.service.dto.ContactApplyDTO;
import com.rem.chat.service.dto.ContactInfoDTO;
import com.rem.chat.service.dto.GroupInfoDTO;
import com.rem.chat.service.dto.UserLoginDTO;
import com.rem.chat.service.enums.UserContactStatusEnum;
import com.rem.chat.service.enums.UserContactTypeEnum;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author: Themberfue
 * @date: 2025/6/4 20:19
 * @description:
 */
public class BeanTransUtil {
    /**
     * @return UserLoginResponse
     */
    public static UserLoginResponse transToUserLoginResponse(UserLoginDTO userLoginDTO, UserInfoDO userInfoDO) {
        UserLoginResponse userLoginResponse = new UserLoginResponse();
        BeanUtils.copyProperties(userLoginDTO, userLoginResponse);
        BeanUtils.copyProperties(userInfoDO, userLoginResponse);
        return userLoginResponse;
    }

    /**
     * @return GroupInfoDO
     */
    public static GroupInfoDO transToGroupInfoDO(GroupSaveRequest param, UserLoginDTO user) {
        GroupInfoDO groupInfoDO = new GroupInfoDO();
        groupInfoDO.setGroupId(param.getGroupId());
        groupInfoDO.setGroupName(param.getGroupName());
        groupInfoDO.setGroupNotice(param.getGroupNotice());
        groupInfoDO.setJoinType(param.getJoinType());
        groupInfoDO.setGroupOwnerId(user.getUserId());
        return groupInfoDO;
    }

    /**
     * @return LoadMyGroupResponse
     */
    public static LoadMyGroupResponse transToLoadGroupInfoResponse(GroupInfoDO groupInfoDO) {
        LoadMyGroupResponse loadMyGroupResponse = new LoadMyGroupResponse();
        BeanUtils.copyProperties(groupInfoDO, loadMyGroupResponse);
        return loadMyGroupResponse;
    }

    /**
     * @return GetGroupInfoResponse
     */
    public static GetGroupInfoResponse transToGetGroupInfoResponse(GroupInfoDO groupInfoDO, String nickName, Long count) {
        GetGroupInfoResponse getGroupInfoResponse = new GetGroupInfoResponse();
        BeanUtils.copyProperties(groupInfoDO, getGroupInfoResponse);
        getGroupInfoResponse.setGroupOwnerNickname(nickName);
        getGroupInfoResponse.setMembers(count);
        return getGroupInfoResponse;
    }

    /**
     * @return GetGroupInfo4ChatResponse
     */
    public static GetGroupInfo4ChatResponse transToGetGroupInfo4ChatResponse(GroupInfoDO groupInfoDO,
                                                                             List<ContactInfoDTO> contactInfoDTOList) {
        List<GetGroupInfo4ChatResponse.UserInfo> userInfoList = contactInfoDTOList.stream()
                .map(BeanTransUtil::transToGetGroupInfo4ChatResponse_UserInfo)
                .toList();

        GetGroupInfo4ChatResponse getGroupInfo4ChatResponse = new GetGroupInfo4ChatResponse();
        getGroupInfo4ChatResponse.setGroupInfo(groupInfoDO);
        getGroupInfo4ChatResponse.setUserInfoList(userInfoList);

        return getGroupInfo4ChatResponse;
    }

    /**
     * @return GetGroupInfo4ChatResponse.UserInfo
     */
    private static GetGroupInfo4ChatResponse.UserInfo transToGetGroupInfo4ChatResponse_UserInfo(ContactInfoDTO contactInfoDTO) {
        GetGroupInfo4ChatResponse.UserInfo userInfo = new GetGroupInfo4ChatResponse.UserInfo();
        BeanUtils.copyProperties(contactInfoDTO, userInfo);
        return userInfo;
    }

    /**
     * @return SearchContactResponse
     */
    public static SearchContactResponse transToSearchContactResponse(UserInfoDO userInfoDO, String userId, Integer status) {
        // Step 1. 校验是否存在
        if (null == userInfoDO) return null;

        // Step 2. status 为空，转化为非好友状态
        if (null == status) {
            status = UserContactStatusEnum.NOT_FRIEND.getStatus();
        }

        SearchContactResponse userContact = SearchContactResponse.builder()
                        .contactName(userInfoDO.getNickName())
                        .contactId(userInfoDO.getUserId())
                        .contactType(UserContactTypeEnum.USER.getDesc())
                        .gender(userInfoDO.getGender())
                        .areaName(userInfoDO.getAreaName())
                        .status(status)
                        .statusDesc(Objects.requireNonNull(
                                UserContactStatusEnum.getByStatus(status)).getDesc())
                        .build();
        // ? 查询自己的情况
        if (userId.equals(userInfoDO.getUserId())) {
            userContact.setStatus(UserContactStatusEnum.FRIEND.getStatus());
            userContact.setStatusDesc(UserContactStatusEnum.FRIEND.getDesc());
        }
        return userContact;
    }

    /**
     * @return SearchContactResponse
     */
    public static SearchContactResponse transToSearchContactResponse(GroupInfoDO groupInfoDO, Integer status) {
        // Step 1. 校验是否存在
        if (null == groupInfoDO) return null;

        // Step 2. status 为空，转化为非好友状态
        if (null == status) {
            status = UserContactStatusEnum.NOT_FRIEND.getStatus();
        }

        return SearchContactResponse.builder()
                .contactId(groupInfoDO.getGroupId())
                .contactName(groupInfoDO.getGroupName())
                .contactType(UserContactTypeEnum.GROUP.getDesc())
                .status(status)
                .statusDesc(Objects.requireNonNull(
                        UserContactStatusEnum.getByStatus(status)).getDesc())
                .build();
    }

    /**
     * @return LoadAllApplyResponse.ContactApplyInfo
     */
    public static LoadAllApplyResponse.ContactApplyInfo transToContactApplyInfo(ContactApplyDTO contactApplyDTO) {
        LoadAllApplyResponse.ContactApplyInfo contactApplyInfo = new LoadAllApplyResponse.ContactApplyInfo();
        BeanUtils.copyProperties(contactApplyDTO, contactApplyInfo);
        return contactApplyInfo;
    }

    /**
     * @return UserInfo
     */
    public static GetUserInfoResponse transToGetUserInfoResponse(UserInfoDO userInfoDO) {
        GetUserInfoResponse infoResponse = new GetUserInfoResponse();
        BeanUtils.copyProperties(userInfoDO, infoResponse);
        return infoResponse;
    }

    /**
     * @return LoadAllUserResponse.UserInfo
     */
    public static LoadAllUserResponse.UserInfo transToUserInfo(UserInfoDO userInfoDO) {
        LoadAllUserResponse.UserInfo infoResponse = new LoadAllUserResponse.UserInfo();
        BeanUtils.copyProperties(userInfoDO, infoResponse);
        return infoResponse;
    }

    /**
     * @return LoadAllBeautyIdResponse.BeautyIdInfo
     */
    public static LoadAllBeautyIdResponse.BeautyIdInfo transToBeautyIdInfo(UserBeautyIdDO userBeautyIdDO) {
        LoadAllBeautyIdResponse.BeautyIdInfo beautyIdInfo = new LoadAllBeautyIdResponse.BeautyIdInfo();
        BeanUtils.copyProperties(userBeautyIdDO, beautyIdInfo);
        return beautyIdInfo;
    }

    /**
     * @return LoadAllGroupResponse.GroupInfo
     */
    public static LoadAllGroupResponse.GroupInfo transToGroupInfo(GroupInfoDTO groupInfoDTO) {
        LoadAllGroupResponse.GroupInfo groupInfo = new LoadAllGroupResponse.GroupInfo();
        BeanUtils.copyProperties(groupInfoDTO, groupInfo);
        return groupInfo;
    }

    /**
     * @return SystemSettingResponse
     */
    public static SystemSettingResponse transToSystemSettingResponse(SysSettingSaveRequest request) {
        SystemSettingResponse response = new SystemSettingResponse();
        BeanUtils.copyProperties(request, response);
        response.setRobotUid(Constants.ROBOT_UID);
        return response;
    }

    public static LoadAllUpdateResponse.UpdateInfo transToUpdateInfo(AppUpdateDO appUpdateDO) {
        LoadAllUpdateResponse.UpdateInfo updateInfo = new LoadAllUpdateResponse.UpdateInfo();
        BeanUtils.copyProperties(appUpdateDO, updateInfo);
        return updateInfo;
    }
}
